home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / DNet / DSendMailDialog.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  7.3 KB  |  325 lines  |  [TEXT/R*ch]

  1. // DSendMailDialog.cp
  2. // d.gilbert
  3.  
  4.  
  5. #include <ncbi.h>
  6. #include <dgg.h>
  7. #include <DControl.h>
  8. #include <DTCP.h>
  9. #include <DSMTPclient.h>
  10. #include <DUtil.h>
  11.  
  12. #include "DSendMailDialog.h"
  13.  
  14. char* gUseraddress = NULL;
  15. char* gMailhost = NULL;
  16. Local Boolean    gCopySelf = false;
  17. Local const char* kCopySelf = "Copy to self";
  18.  
  19.  
  20. //#define    Inherited    DWindow
  21.  
  22. DSendMailDialog::DSendMailDialog(long id, DTaskMaster* itsSuperior,
  23.         short width, short height, short left, short top, char* title) :
  24.     DWindow( id, itsSuperior, fixed, width, height, left, top, title, kFreeOnClose)
  25. {
  26.     fTo = fFrom = fSubj = fCCopy= NULL; 
  27.     fMsg =  NULL; 
  28.  
  29.     if (!gMailhost || !gUseraddress) {
  30.         gMailhost= gPrefManager->GetPref("MAILHOST", "MAIL");
  31.         gUseraddress= gPrefManager->GetPref("USERADDRESS", "MAIL");
  32.         }
  33.     fMailhost= StrDup(gMailhost);
  34. }
  35.  
  36. DSendMailDialog::~DSendMailDialog() 
  37. {
  38.     if (fMailhost) MemFree(fMailhost);
  39. }
  40.  
  41.  
  42. void DSendMailDialog::SetDlogItems( DView* to, DView* from, DView* ccopy, 
  43.                         DView* subj, DView* msg) 
  44. {
  45.     fTo     = to;
  46.     fFrom = from;
  47.     fSubj = subj;
  48.     fCCopy= ccopy;  
  49.     fMsg     = msg;
  50. }
  51.  
  52. void  DSendMailDialog::SetMailhost(char* hostname)
  53. {
  54.     if (fMailhost) MemFree(fMailhost);
  55.     fMailhost= StrDup(hostname);
  56. }
  57.  
  58.  
  59. char* DSendMailDialog::BuildMessage()
  60. {
  61.     if (fMsg) return fMsg->GetText(); 
  62.     else return NULL;
  63. }
  64.  
  65. void DSendMailDialog::SendMessage()
  66. {    
  67.     char    *to = NULL, *from = NULL, *ccopy = NULL, *subj = NULL, 
  68.                 *msg = NULL, *mailhost = NULL;
  69.  
  70.     if (fTo)         to    = fTo->GetText(); 
  71.     if (fFrom)     from= fFrom->GetText(); 
  72.     if (fSubj)     subj= fSubj->GetText(); 
  73.     
  74.     if (fCCopy) {
  75.         ccopy= fCCopy->GetText(); 
  76.         if (StrICmp(ccopy, kCopySelf) == 0) {
  77.             ccopy= (char*) MemFree(ccopy);
  78.             Boolean curcopy= fCCopy->GetStatus();
  79.             if (curcopy != gCopySelf) {
  80.                 gCopySelf= curcopy;
  81.                 gPrefManager->SetPref( (gCopySelf) ? "1":"0" ,"COPYSELF", "MAIL");
  82.                 }
  83.             if (gCopySelf) ccopy= StrDup(from);
  84.             }
  85.         }
  86.         
  87.     msg= BuildMessage();  // subclasses can build their own message to send
  88.     
  89.     if (fMailhost) mailhost= fMailhost;
  90.     else if (from) {
  91.         mailhost= StrChr( from, '@'); // a quick hack -- won't work frequently
  92.         if (mailhost) ++mailhost;
  93.         }
  94.     if (mailhost) {
  95.         DSMTP* smailer = new DSMTP(mailhost);
  96.         smailer->SendMail( to, from, subj, ccopy, msg);
  97.         delete smailer;
  98.         }
  99.     else 
  100.         Message(MSG_OK, "Can't find mail host");
  101.         
  102.     MemFree( to);
  103.     MemFree( from);
  104.     MemFree( ccopy);
  105.     MemFree( subj);
  106.     MemFree( msg);
  107. }
  108.  
  109.  
  110. Boolean DSendMailDialog::IsMyAction(DTaskMaster* action) 
  111. {    
  112.     switch(action->Id()) {
  113.         case cSEND:
  114.             gCursor->watch();
  115.             this->SendMessage();
  116.             // fall thru to next case
  117.         case cCANC:
  118.             this->CloseAndFree();
  119.             gCursor->arrow();
  120.             return true;
  121.         default:
  122.             return DWindow::IsMyAction(action);    
  123.         }
  124. }
  125.  
  126. DView* DSendMailDialog::InstallTo(DView* super, char* toStr)
  127. {
  128.     if (fTo)
  129.         return fTo;
  130.     else {
  131.         DPrompt* pr= new DPrompt(0, super, "To:", 0, 0, Nlm_programFont);
  132.         super->NextSubviewToRight();
  133.     
  134.         DDialogText* eto= new DEditText(cTo, super, toStr, 20);
  135.         this->SetEditText(eto);
  136.         super->NextSubviewBelowLeft();
  137.         fTo= eto;
  138.         return eto;
  139.         }
  140. }
  141.  
  142. DView* DSendMailDialog::InstallFrom(DView* super, char* fromStr)
  143. {
  144.     if (fFrom)
  145.         return fFrom;
  146.     else {
  147.         DPrompt* pr= new DPrompt(0, super, "From:", 0, 0, Nlm_programFont);
  148.         super->NextSubviewToRight();
  149.         
  150.         char* myaddr = (gUseraddress) ? gUseraddress : fromStr;
  151.         DDialogText* efrom= new DEditText(cFrom, super, myaddr, 20);
  152.         this->SetEditText(efrom);
  153.         super->NextSubviewBelowLeft();
  154.         fFrom= efrom;
  155.         return efrom;
  156.         }
  157. }
  158.  
  159. DView* DSendMailDialog::InstallSubject(DView* super, char* subjStr)
  160. {
  161.     if (fSubj)
  162.         return fSubj;
  163.     else {
  164.         DPrompt* pr= new DPrompt(0, super, "Subject:", 0, 0, Nlm_programFont);
  165.         super->NextSubviewToRight();
  166.     
  167.         DDialogText* esubj= new DEditText(cSubj, super, subjStr, 20);
  168.         this->SetEditText(esubj);
  169.         super->NextSubviewBelowLeft();
  170.         fSubj= esubj;
  171.         return esubj;
  172.         }
  173. }
  174.  
  175. DView* DSendMailDialog::InstallCCopy(DView* super, char* ccopyStr)
  176. {
  177.     if (ccopyStr) {
  178.         DPrompt* pr= new DPrompt(0, super, "Copy to:", 0, 0, Nlm_programFont);
  179.         super->NextSubviewToRight();
  180.         DDialogText* esubj= new DEditText(cCCopy, super, ccopyStr, 20);
  181.         this->SetEditText(esubj);
  182.         super->NextSubviewBelowLeft();
  183.         return esubj;
  184.         }
  185.     else {
  186.         DCheckBox* chk= new DCheckBox( cCopySelf, super, (char*)kCopySelf);
  187.         chk->SetStatus(gCopySelf);
  188.         super->NextSubviewBelowLeft();
  189.         return chk;
  190.         }
  191. }
  192.  
  193.  
  194. DView* DSendMailDialog::InstallMessage(DView* super, char* msgStr)
  195. {
  196.     if (fMsg)
  197.         return fMsg;
  198.     else {
  199.         DDialogScrollText* emsg = new DDialogScrollText(cMsg, super,
  200.                                                          25, 6, Nlm_programFont, true/*kDoWrap*/);
  201.         SetEditText(emsg); 
  202.         if (msgStr) emsg->SetTitle(msgStr);
  203.         fMsg= emsg;
  204.         return emsg;
  205.         }
  206. }
  207.  
  208.  
  209.  
  210. void DSendMailDialog::Open()
  211. {
  212.     DView *eto, *efrom, *esubj, *eccopy, *emsg;
  213.  
  214.     eto  = this->InstallTo(this);
  215.     efrom= this->InstallFrom(this);
  216.     esubj= this->InstallSubject(this);
  217.     eccopy= this->InstallCCopy(this);
  218.     emsg= this->InstallMessage(this);
  219.     SetDlogItems( eto, efrom, eccopy, esubj, emsg);
  220.     AddOkayCancelButtons(cSEND,"Send");
  221.     DWindow::Open();
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. // class DMailSetupDialog
  230.  
  231.  
  232. DMailSetupDialog::DMailSetupDialog(long id, DTaskMaster* itsSuperior,
  233.         short width, short height, short left, short top, char* title) :
  234.     DWindow( id, itsSuperior, fixed, width, height, left, top, title, kFreeOnClose)
  235. {
  236.     fMailhost = fUseraddress= NULL; 
  237.     GetDefaults();
  238. }
  239.  
  240. DMailSetupDialog::~DMailSetupDialog() 
  241. {
  242. }
  243.  
  244.  
  245. void DMailSetupDialog::GetDefaults() 
  246. {
  247.     if (!gMailhost || !gUseraddress) {
  248.         gMailhost      = gPrefManager->GetPref("MAILHOST", "MAIL",">> mail.sending.computer <<");
  249.         gUseraddress= gPrefManager->GetPref("USERADDRESS", "MAIL", ">> YourAcct@Your.Mail.Host <<");
  250.         char* copyself= gPrefManager->GetPref("COPYSELF", "MAIL","0");
  251.         if (copyself) gCopySelf= (*copyself == '1');
  252.         }
  253. }
  254.  
  255. void DMailSetupDialog::SetDlogItems( DView* host, DView* address) 
  256. {
  257.     fMailhost     = host;
  258.     fUseraddress = address;
  259. }
  260.  
  261. Boolean DMailSetupDialog::IsMyAction(DTaskMaster* action) 
  262. {    
  263.     switch(action->Id()) {
  264.          
  265.         case cOKAY:
  266.             if (fMailhost)    {
  267.                 if (gMailhost) MemFree(gMailhost);
  268.                 gMailhost    = fMailhost->GetText(); 
  269.                 gPrefManager->SetPref( gMailhost, "MAILHOST", "MAIL");
  270.                 }
  271.             if (fUseraddress) {
  272.                 if (gUseraddress) MemFree(gUseraddress);
  273.                 gUseraddress= fUseraddress->GetText(); 
  274.                 gPrefManager->SetPref( gUseraddress, "USERADDRESS", "MAIL");
  275.                 }
  276.             // fall thru to next case
  277.             
  278.         case cCANC:
  279.             this->CloseAndFree();
  280.             return true;
  281.             
  282.         default:
  283.             return DWindow::IsMyAction(action);    
  284.         }
  285. }
  286.  
  287. DView* DMailSetupDialog::InstallHost(DView* super)
  288. {
  289.     DPrompt* pr= new DPrompt(0, super, "Send mail host:", 0, 0, Nlm_programFont);
  290.     super->NextSubviewToRight();
  291.  
  292.     char* myhost = (gMailhost) ? gMailhost : ">> some.computer.address <<";
  293.     DDialogText* eto= new DEditText(cTo, super, myhost, 20);
  294.     this->SetEditText(eto);
  295.     super->NextSubviewBelowLeft();
  296.     return eto;
  297. }
  298.  
  299. DView* DMailSetupDialog::InstallAddress(DView* super)
  300. {
  301.     DPrompt* pr= new DPrompt(0, super, "Your mail address:", 0, 0, Nlm_programFont);
  302.     super->NextSubviewToRight();
  303.     
  304.     char* myaddr = (gUseraddress) ? gUseraddress : ">> your@email.address.here <<";
  305.     DDialogText* efrom= new DEditText(cFrom, super, myaddr, 20);
  306.     this->SetEditText(efrom);
  307.     super->NextSubviewBelowLeft();
  308.     return efrom;
  309. }
  310.  
  311.  
  312.  
  313. void DMailSetupDialog::Open()
  314. {
  315.     DView *address, *mailhost;
  316.     mailhost= this->InstallHost(this);
  317.     address = this->InstallAddress(this);
  318.     SetDlogItems( mailhost, address);
  319.     AddOkayCancelButtons();
  320.     DWindow::Open();
  321. }
  322.  
  323.  
  324.  
  325.